package com.xjm.ips.controller.system.basic;

import com.xjm.ips.entity.AjaxJson;
import com.xjm.ips.entity.Position;
import com.xjm.ips.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {
    @Autowired
    PositionService positionService;

    @GetMapping("/")
    public List<Position> getAllPositions(){
        return positionService.getAllPositions();
    }

    @PostMapping("/")
    public AjaxJson addPosition(@RequestBody Position position){
        if(positionService.addPositon(position) == 1){
            return AjaxJson.ok("添加成功!");
        }
            return AjaxJson.error("添加失败!");
    }

    @PutMapping("/")
    public AjaxJson updatePosition(@RequestBody Position position){
        if(positionService.updatePosition(position) == 1){
            return  AjaxJson.ok("编辑成功!");
        }
        return AjaxJson.error("编辑失败!");
    }

    @DeleteMapping("/{id}")
    public AjaxJson deletePositionById(@PathVariable Integer id){
        if (positionService.deletePositionById(id) == 1) {
            return AjaxJson.ok("删除成功!");
        }
        return AjaxJson.error("删除失败!该职位可能还有员工任职!");
    }

    @DeleteMapping("/batch")
    public AjaxJson deletePositionsByIds(Integer[] ids){
        if(positionService.deletePositionsByIds(ids) == ids.length){
            return AjaxJson.ok("批量删除成功!");
        }
        return AjaxJson.error("批量删除失败!");
    }
}
